﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DeadLineMethods.cs" company="hello_world">
//   无版权
// </copyright>
// <summary>
//   Defines the DeadLineMethods type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WindowsFormsApp1
{
    using System;
    using System.Collections.Generic;
    using System.Data.SQLite;

    /// <summary>
    /// The dead line methods.
    /// </summary>
    internal class DeadLineMethods
    {
        /// <summary>
        /// The select by time.
        /// </summary>
        /// <param name="date">
        /// The date.
        /// </param>
        /// <returns>
        /// The <see>
        ///     <cref>List</cref>
        /// </see>
        /// .
        /// </returns>
        public static List<Deadline> SelectByTime(DateTime date)
        {
            var list = new List<Deadline>();
            var stringDate = date.ToString("yyyy-MM-dd");

            using (var connection = new SQLiteConnection(@"Data Source=..\..\..\..\mfc.db"))
            {
                connection.Open();

                var command = connection.CreateCommand();
                command.CommandText =
                    @"SELECT deadline,task,schedule.name FROM deadline ,schedule WHERE deadline=$ddl AND schedule.courseId=deadline.courseId";
                command.Parameters.AddWithValue("$ddl", stringDate);
                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var deadlineTime = reader["deadline"].ToString();
                        var task = reader["task"].ToString();
                        var courseName = reader["name"].ToString();
                        var deadline = new Deadline
                        {
                            DeadlineTime = deadlineTime,
                            Information = task,
                            Course = courseName
                        };
                        list.Add(deadline);
                    }
                }
            }

            return list;
        }

        /// <summary>
        /// The select all.
        /// </summary>
        /// <returns>
        /// The <see>
        ///     <cref>List</cref>
        /// </see>
        /// .
        /// </returns>
        public static List<string> SelectAllDeadline()
        {
            var list = new List<string>();
            using (var connection = new SQLiteConnection(@"Data Source=..\..\..\..\mfc.db"))
            {
                connection.Open();

                var command = connection.CreateCommand();
                command.CommandText = @"SELECT deadline FROM deadline";
                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var ddl = reader["deadline"].ToString();
                        list.Add(ddl);
                    }
                }
            }

            return list;
        }

        public static List<Deadline> SelectAll()
        {
            var list=new List<Deadline>();
            using (var connection=new SQLiteConnection(@"Data Source=..\..\..\..\mfc.db"))
            {
                connection.Open();

                var command = connection.CreateCommand();
                command.CommandText =
                    @"SELECT deadline,task,schedule.name FROM deadline,schedule WHERE schedule.courseId=deadline.courseId";
                using (var reader=command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var deadlineTime = reader["deadline"].ToString();
                        var task = reader["task"].ToString();
                        var courseName = reader["name"].ToString();
                        list.Add(new Deadline{DeadlineTime =deadlineTime,Information = task,Course = courseName});
                    }
                }
            }

            return list;
        }
    }
}
